/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelQuackey.java
 *
 * Created on Mar 5, 2010, 9:44:55 PM
 */

package com.revoltingdevelopment.perquackey;

/**
 *
 * @author bovious
 */
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class PanelQuackey extends javax.swing.JFrame {

    /** Creates new form PanelQuackey */
    public PanelQuackey() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        rollButton = new javax.swing.JButton();
        vulnerableIndicator = new javax.swing.JRadioButton();
        shuffleButton = new javax.swing.JButton();
        wordPlayed = new javax.swing.JTextField();
        resultsField = new javax.swing.JTextField();
        playTheWordButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextPane1.setEditable(false);
        jTextPane1.setFocusable(false);
        jScrollPane1.setViewportView(jTextPane1);

        rollButton.setText("Roll");
        rollButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rollButtonActionPerformed(evt);
            }
        });

        vulnerableIndicator.setText("Vulnerable");
        vulnerableIndicator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vulnerableIndicatorActionPerformed(evt);
            }
        });

        shuffleButton.setText("Shuffle");
        shuffleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shuffleButtonActionPerformed(evt);
            }
        });

        wordPlayed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wordPlayedActionPerformed(evt);
            }
        });
        wordPlayed.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                wordPlayedKeyTyped(evt);
            }
        });

        resultsField.setEditable(false);

        playTheWordButton.setText("Play the word");
        playTheWordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playTheWordButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(230, 230, 230)
                        .addComponent(vulnerableIndicator))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(rollButton)
                            .addComponent(resultsField)
                            .addComponent(wordPlayed))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(shuffleButton))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(playTheWordButton)))))
                .addContainerGap(88, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rollButton)
                    .addComponent(shuffleButton))
                .addGap(18, 18, 18)
                .addComponent(vulnerableIndicator)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wordPlayed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(playTheWordButton))
                .addGap(18, 18, 18)
                .addComponent(resultsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(163, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rollButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rollButtonActionPerformed
        myRoll = new PerquackeyDiceRoll();
        firstTen = new String(myRoll.showNormal());
        if (isVuln) {
            vulnerableLetters = new String(myRoll.showVulnerable());
            jTextPane1.setText(firstTen + vulnerableLetters);
        }
        else    {
            jTextPane1.setText(firstTen);
        }
}//GEN-LAST:event_rollButtonActionPerformed

    private void vulnerableIndicatorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vulnerableIndicatorActionPerformed
        isVuln = !isVuln;
}//GEN-LAST:event_vulnerableIndicatorActionPerformed

    private void shuffleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shuffleButtonActionPerformed
        // Shuffle the letters
        String temp = new String(myRoll.showNormal());

        if (isVuln) {
            String myVulnerableString = new String(myRoll.showVulnerable());
            temp = temp + myVulnerableString;
        }

        String[] myReturnArray = temp.split("(?!^)");

        List<String> list = Arrays.asList(myReturnArray);

        Collections.shuffle(list);

        String myReturn = list.toString();

        // Get rid of the list notation within list.toString
        System.out.println(myReturn.replaceAll("\\W+", ""));
        jTextPane1.setText(myReturn.replaceAll("\\W+", ""));

}//GEN-LAST:event_shuffleButtonActionPerformed

    private void wordPlayedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wordPlayedActionPerformed
        // TODO add your handling code here:
        resultsField.setText(wordPlayed.getText());

}//GEN-LAST:event_wordPlayedActionPerformed

    private void playTheWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playTheWordButtonActionPerformed
        // TODO This truly seems not to be working at all..
        
        String theWord = wordPlayed.getText();
        if (myRoll.isInRoll(theWord,false)) {
           resultsField.setText(wordPlayed.getText());
        }
        else    {
            resultsField.setText("Sorry");
        }
 
}//GEN-LAST:event_playTheWordButtonActionPerformed

    private void wordPlayedKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_wordPlayedKeyTyped
//        // TODO create a method to examine each letter pressed for cup validity
  //      // probably a good method to add to PerquackeyDiceRoll
    //    String thisWord = wordPlayed.getText();
      //  if (myRoll.isInRoll(thisWord,false))  {

        //    resultsField.setText(thisWord);
        return;
        //  resultsField.setText("You struck a key");
 //       }
    }//GEN-LAST:event_wordPlayedKeyTyped

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PanelQuackey().setVisible(true);
            }
        });
    }
    // My variables declarations
    private PerquackeyDiceRoll myRoll;
    private boolean isVuln = false;
    private String firstTen;
    private String vulnerableLetters;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JButton playTheWordButton;
    private javax.swing.JTextField resultsField;
    private javax.swing.JButton rollButton;
    private javax.swing.JButton shuffleButton;
    private javax.swing.JRadioButton vulnerableIndicator;
    private javax.swing.JTextField wordPlayed;
    // End of variables declaration//GEN-END:variables

}
